using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace GameWP
{
    public class SettingComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game _game;
        SpriteBatch _spriteBatch;
        SpriteFont _menuFont, _textFont;
        Vector2 touchPosition, pretouchPosition;
        List<Button> _listItemMenu;
        Button _info;
        Button _sound, _level, _reset;
        public List<Button> ListItemMenu
        {
            get { return _listItemMenu; }
            set { _listItemMenu = value; }
        }


        //Initilation
        public SettingComponent(Game game, SpriteFont spriteFont, SpriteFont textFont, SpriteBatch spriteBatch)
            : base(game)
        {
            _game = game;
            _menuFont = spriteFont;
            _textFont = textFont;
            _spriteBatch = spriteBatch;

            //Init list menu item
            string infoText = "- Sound \n\n- Level";
            ListItemMenu = new List<Button>();
            ListItemMenu.Add(new Button("Back", _menuFont, new Vector2(690, 410), Color.Red));
            _info = new Button(infoText, _textFont, new Vector2(400,200), Color.White) { HaveAnimation = false };
            _sound = new Button("On", _textFont, new Vector2(550, 240), Color.Yellow) { HaveShadow = false };
            _sound.Selected += new Button.IsSelected(_sound_Selected);
            _level = new Button("Normal", _textFont, new Vector2(550, 330), Color.Yellow) { HaveShadow = false };
            _level.Selected += new Button.IsSelected(_level_Selected);
            _reset = new Button("Reset data", _textFont, new Vector2(50, 50), Color.Red);
            _reset.Selected += new Button.IsSelected(_reset_Selected);
        }

        void _reset_Selected(int input)
        {
            //Reset all data lufy
            ManagerGame.luffy.Hp = ManagerGame.managerSaveLoad._dataFactory._hp;
            ManagerGame.luffy.Mana = ManagerGame.managerSaveLoad._dataFactory._mana;
            ManagerGame.luffy.Damage = ManagerGame.managerSaveLoad._dataFactory._dam;
            ManagerGame.luffy.Balance = ManagerGame.managerSaveLoad._dataFactory._balance;
            ManagerGame.luffy.Level = ManagerGame.managerSaveLoad._dataFactory._level;
            ManagerGame.luffy.LevelSkill = ManagerGame.managerSaveLoad._dataFactory._levelSkill;
            ManagerGame.luffy.LevelScreen = ManagerGame.managerSaveLoad._dataFactory._levelScreen;
            ManagerGame.luffy.ListAHeroHave.Clear();
        }

        void _level_Selected(int input)
        {
            if (_level.Text == "Normal")
            {
                _level.Text = "Difficult";
                ManagerGame.LevelNormal = false;
            }
            else
            { _level.Text = "Normal"; ManagerGame.LevelNormal = true; }
        }

        void _sound_Selected(int input)
        {
            if (_sound.Text == "On")
            {
                _sound.Text = "Off";
                ManagerGame.SoundOn = false;
                MediaPlayer.Stop();
            }
            else
            { _sound.Text = "On"; ManagerGame.SoundOn = true; MediaPlayer.Play(ManagerGame.managerSound._menuStart); };
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
                base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here    
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                _game.Exit();
            //Key input
            TouchCollection touchCollection = TouchPanel.GetState();
            touchPosition = Vector2.Zero;
            if (touchCollection.Count > 0)
            {
                for (int i = 0; i < touchCollection.Count;i++ )
                    if (pretouchPosition == touchCollection[i].Position) 
                        return;
                touchPosition = touchCollection[0].Position;                
                pretouchPosition = touchPosition;
            }

            for (int i = 0; i < ListItemMenu.Count; i++)
                ListItemMenu[i].ButtonIsTouched(touchPosition, i);

            float timeInSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (TouchPanel.IsGestureAvailable)
            {
                // Read in the Gesture input
                GestureSample gesture = TouchPanel.ReadGesture();

                // Process Gestures
                if (gesture.GestureType == GestureType.Tap)
                {
                    _sound.ButtonIsTouched(gesture.Position, 0);
                    _level.ButtonIsTouched(gesture.Position, 0);
                    _reset.ButtonIsTouched(gesture.Position, 0);
                }
            }
            
                base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            _info.Draw(_spriteBatch, gameTime);
            _sound.Draw(_spriteBatch, gameTime);
            _level.Draw(_spriteBatch, gameTime);
            _reset.Draw(_spriteBatch, gameTime);
            for (int i = 0; i < ListItemMenu.Count; i++)
                ListItemMenu[i].Draw(_spriteBatch, gameTime);
        }
    }
}
